// 6.44 编写递归算法：求二叉树中以元素值为x的结点为根的子树的深度。
#include <stdio.h>
#include <stdlib.h>
#include "E:\Desktop\data_struct\5TREE\1_bin_tree\creat.h"
// return the depth of root
int fun_6_44_1(tree *root)
{
    if (!root)
        return 0;
    else
    {
        int l = fun_6_44_1(root->left);
        int r = fun_6_44_1(root->right);
        return l > r ? l + 1 : r + 1;
    }
}
// locator
tree *fun_6_44_2(tree *root, int key)
{
    if (root)
    {
        if (root->data == key)
            return root;
        tree *a = fun_6_44_2(root->left, key);
        tree *b = fun_6_44_2(root->right, key);
        if (a && a->data == key)
            return a;
        else if (b && b->data == key)
            return b;
    }
}
// suppose only one node->data == key
int fun_6_44(tree *root, int key)
{
    tree *p = fun_6_44_2(root, key);
    return fun_6_44_1(p);
}

int main()
{
    /*******************code*******************/
    int n = 15;
    int a1[15] = {1,
                  2, 3,
                  4, 5, NULL, NULL,
                  NULL, 6, 7, NULL, NULL, NULL, NULL, NULL};
    tree *root;
    CreatTree(root, a1, n, 0);
    printf("%d", fun_6_44(root, 2));
    /******************************************/
    printf("\n\n****************************\n");
    printf("Press Enter key to continue\n");
    getchar();
    return 0;
    /******************************************/
}
